// pages/siji_order/my_order.js
import {getOrderMyOrder} from '../../api/user'
import {postOrdersCompltet,postOrdersSettle} from '../../api/order'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        parameter: {
            'navbar': '1',
            'return': '2',
            'title': '我的订单'
          },
          navH:'',

          type_id:0,
          list:[],
          page:1,
          loading:true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            navH: getApp().globalData.navHeight
         });
        
    },
     //切换
     qiehuan(e){
        let id = e.currentTarget.dataset.id;
        let that =this;
        that.setData({
             type_id:id,
             page:1,
             loading:true
        },function(){
            wx.showLoading({
              title: ' ',
            })
            that.getlist()
        })
    },
     //订单列表
     getlist(){
        let data ={
            page:this.data.page,
            limit:10,
            type:this.data.type_id
        }
        getOrderMyOrder(data).then(res=>{
            wx.hideLoading()
             if(this.data.page==1){
                 this.setData({
                     list:res.data,
                     loading:res.data.length>=10
                 })
             }else{
                this.setData({
                    list:this.data.list.concat(res.data),
                    loading:res.data.length>=10
                })
             }
        }).catch(e=>{}) 
    },
     //复制
     fuzhi(e){
        let id = e.currentTarget.dataset.id;
         wx.setClipboardData({
              data: id,
              success: function (res) {
                  getApp().Tips({title:'复制成功'});
              }
          })
    },
     //详情
     go_xq(e){
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: './details?id='+id,
        })   
    },
    //修改订单
    edit_order(e){
         let id = e.currentTarget.dataset.id;
         wx.navigateTo({
           url: './edit?id='+id
         })
    },
    //确认完成
    qr_order(e){
        let id = e.currentTarget.dataset.id;
        let index = e.currentTarget.dataset.index;
        let that =this;
        wx.showModal({
          title: '大象搬家',
          content: '已和客户确认该订单已完成',
          complete: (res) => { 
            if (res.confirm) {
                postOrdersCompltet({oid:id}).then(res=>{
                    getApp().Tips({title:res.msg})
                    that.data.list.splice(index,1)
                    that.setData({
                        list:that.data.list
                    })  
                }).catch(e=>{
                    getApp().Tips({title:e})
                })
            }
          }
        })
    },

    //结算订单
    js_order(e){
        let id = e.currentTarget.dataset.id;
        let index = e.currentTarget.dataset.index;
        let that =this;
        wx.showLoading({
          title: '正在结算中..',
        })
        postOrdersSettle({oid:id}).then(res=>{
            wx.hideLoading()
            getApp().Tips({title:res.msg})
            let that =this;
            that.setData({
                loading:true,
                page:1
            },function(){
                that.getlist()
            })
        }).catch(e=>{
            wx.hideLoading()
            getApp().Tips({title:e})
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let that =this;
        that.setData({
            loading:true,
            page:1
        },function(){
            that.getlist()
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        if(this.data.loading){
            let that = this;
            that.setData({
                page:that.data.page+1,
                loading:false
            },function(){
                that.getlist()
            })
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})